Basic Python and native data structures: exercises

Define a function max() that takes two numbers as arguments and returns the largest of them.


In [2]:
def max(number1, number2):
    if number1 > number2:
        return number1
    else:
        return number2
    
max(1, 2)


Out[2]:
2

In [3]:
max(100, 10)


Out[3]:
100

Write a function find_longest_word() that takes a list of words and returns the length of the longest one


In [13]:
def find_longest_word(word_list):
    
    max_word=0
    
    for w in word_list:
        if len(w) > max_word:
            max_word = len(w)
    return max_word

l = ['uydfguyfg', 'ffefu', "kuurhr", "hggug", "hgrhggrel"]

find_longest_word(l)


Out[13]:
9

Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n.


In [7]:
words_list = ['banana', 'apple', 'orange', 'elephant', 'raspberry']
min_length = 7
# with list comprehension: elegant and concise
[word for word in words_list if len(word) >= min_length]


Out[7]:
['elephant', 'raspberry']

In [8]:
# with a function: more explicit and simpler
def filter_long_words(words_list, min_length):
    output = []
    for word in words_list:
        if len(word) >= min_length:
            output.append(word)
    return output

In [9]:
filter_long_words(words_list, 7)


Out[9]:
['elephant', 'raspberry']

Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of the chosen character. For example, generate_n_chars(5,"x") should return the string "xxxxx".


In [1]:
def generate_n_chars(n, c):
    return c * n

generate_n_chars(5, 'x')


Out[1]:
'xxxxx'

Write a program that takes list of words and returns a dictionary with the words as keys and their length as values.


In [2]:
def generate_dictionary(words):
    dictionary = {}
    for word in words:
        dictionary[word] = len(word)
    return dictionary

generate_dictionary(['python', 'blast', 'banana'])


Out[2]:
{'banana': 6, 'blast': 5, 'python': 6}

A pangram is a sentence that contains all the letters of the English alphabet at least once, for example: The quick brown fox jumps over the lazy dog. Your task here is to write a function to check a sentence to see if it is a pangram or not.


In [4]:
# you can create a set from a string
set('abcdef')


Out[4]:
{'a', 'b', 'c', 'd', 'e', 'f'}

In [15]:
a = set('abc')
a.issubset(set('abcd'))


Out[15]:
True

In [20]:
def check_pangram(sentence):
    letters = set('abcdefghijklmnopqrstuvwxyz')
    found = set()
    for char in sentence.lower():
        found.add(char)
    # check if our letters is a subset of all english letters
    if letters < found:
        return True
    else:
        return False

In [21]:
check_pangram('The quick brown fox jumps over the lazy dog')


Out[21]:
True

"99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. The song's simple lyrics are as follows:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.

The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero.

Your task here is write a function capable of generating all the verses of the song.


In [25]:
def sing():
    verse1 = '{0} bottles of beer on the wall, {0} bottles of beer.'
    verse2 = 'Take one down, pass it around, {0} bottles of beer on the wall.'
    bottles = 99
    while bottles > 0:
        print(verse1.format(bottles))
        print(verse2.format(bottles-1))
        bottles -= 1

In [26]:
sing()


99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down, pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer.
Take one down, pass it around, 96 bottles of beer on the wall.
96 bottles of beer on the wall, 96 bottles of beer.
Take one down, pass it around, 95 bottles of beer on the wall.
95 bottles of beer on the wall, 95 bottles of beer.
Take one down, pass it around, 94 bottles of beer on the wall.
94 bottles of beer on the wall, 94 bottles of beer.
Take one down, pass it around, 93 bottles of beer on the wall.
93 bottles of beer on the wall, 93 bottles of beer.
Take one down, pass it around, 92 bottles of beer on the wall.
92 bottles of beer on the wall, 92 bottles of beer.
Take one down, pass it around, 91 bottles of beer on the wall.
91 bottles of beer on the wall, 91 bottles of beer.
Take one down, pass it around, 90 bottles of beer on the wall.
90 bottles of beer on the wall, 90 bottles of beer.
Take one down, pass it around, 89 bottles of beer on the wall.
89 bottles of beer on the wall, 89 bottles of beer.
Take one down, pass it around, 88 bottles of beer on the wall.
88 bottles of beer on the wall, 88 bottles of beer.
Take one down, pass it around, 87 bottles of beer on the wall.
87 bottles of beer on the wall, 87 bottles of beer.
Take one down, pass it around, 86 bottles of beer on the wall.
86 bottles of beer on the wall, 86 bottles of beer.
Take one down, pass it around, 85 bottles of beer on the wall.
85 bottles of beer on the wall, 85 bottles of beer.
Take one down, pass it around, 84 bottles of beer on the wall.
84 bottles of beer on the wall, 84 bottles of beer.
Take one down, pass it around, 83 bottles of beer on the wall.
83 bottles of beer on the wall, 83 bottles of beer.
Take one down, pass it around, 82 bottles of beer on the wall.
82 bottles of beer on the wall, 82 bottles of beer.
Take one down, pass it around, 81 bottles of beer on the wall.
81 bottles of beer on the wall, 81 bottles of beer.
Take one down, pass it around, 80 bottles of beer on the wall.
80 bottles of beer on the wall, 80 bottles of beer.
Take one down, pass it around, 79 bottles of beer on the wall.
79 bottles of beer on the wall, 79 bottles of beer.
Take one down, pass it around, 78 bottles of beer on the wall.
78 bottles of beer on the wall, 78 bottles of beer.
Take one down, pass it around, 77 bottles of beer on the wall.
77 bottles of beer on the wall, 77 bottles of beer.
Take one down, pass it around, 76 bottles of beer on the wall.
76 bottles of beer on the wall, 76 bottles of beer.
Take one down, pass it around, 75 bottles of beer on the wall.
75 bottles of beer on the wall, 75 bottles of beer.
Take one down, pass it around, 74 bottles of beer on the wall.
74 bottles of beer on the wall, 74 bottles of beer.
Take one down, pass it around, 73 bottles of beer on the wall.
73 bottles of beer on the wall, 73 bottles of beer.
Take one down, pass it around, 72 bottles of beer on the wall.
72 bottles of beer on the wall, 72 bottles of beer.
Take one down, pass it around, 71 bottles of beer on the wall.
71 bottles of beer on the wall, 71 bottles of beer.
Take one down, pass it around, 70 bottles of beer on the wall.
70 bottles of beer on the wall, 70 bottles of beer.
Take one down, pass it around, 69 bottles of beer on the wall.
69 bottles of beer on the wall, 69 bottles of beer.
Take one down, pass it around, 68 bottles of beer on the wall.
68 bottles of beer on the wall, 68 bottles of beer.
Take one down, pass it around, 67 bottles of beer on the wall.
67 bottles of beer on the wall, 67 bottles of beer.
Take one down, pass it around, 66 bottles of beer on the wall.
66 bottles of beer on the wall, 66 bottles of beer.
Take one down, pass it around, 65 bottles of beer on the wall.
65 bottles of beer on the wall, 65 bottles of beer.
Take one down, pass it around, 64 bottles of beer on the wall.
64 bottles of beer on the wall, 64 bottles of beer.
Take one down, pass it around, 63 bottles of beer on the wall.
63 bottles of beer on the wall, 63 bottles of beer.
Take one down, pass it around, 62 bottles of beer on the wall.
62 bottles of beer on the wall, 62 bottles of beer.
Take one down, pass it around, 61 bottles of beer on the wall.
61 bottles of beer on the wall, 61 bottles of beer.
Take one down, pass it around, 60 bottles of beer on the wall.
60 bottles of beer on the wall, 60 bottles of beer.
Take one down, pass it around, 59 bottles of beer on the wall.
59 bottles of beer on the wall, 59 bottles of beer.
Take one down, pass it around, 58 bottles of beer on the wall.
58 bottles of beer on the wall, 58 bottles of beer.
Take one down, pass it around, 57 bottles of beer on the wall.
57 bottles of beer on the wall, 57 bottles of beer.
Take one down, pass it around, 56 bottles of beer on the wall.
56 bottles of beer on the wall, 56 bottles of beer.
Take one down, pass it around, 55 bottles of beer on the wall.
55 bottles of beer on the wall, 55 bottles of beer.
Take one down, pass it around, 54 bottles of beer on the wall.
54 bottles of beer on the wall, 54 bottles of beer.
Take one down, pass it around, 53 bottles of beer on the wall.
53 bottles of beer on the wall, 53 bottles of beer.
Take one down, pass it around, 52 bottles of beer on the wall.
52 bottles of beer on the wall, 52 bottles of beer.
Take one down, pass it around, 51 bottles of beer on the wall.
51 bottles of beer on the wall, 51 bottles of beer.
Take one down, pass it around, 50 bottles of beer on the wall.
50 bottles of beer on the wall, 50 bottles of beer.
Take one down, pass it around, 49 bottles of beer on the wall.
49 bottles of beer on the wall, 49 bottles of beer.
Take one down, pass it around, 48 bottles of beer on the wall.
48 bottles of beer on the wall, 48 bottles of beer.
Take one down, pass it around, 47 bottles of beer on the wall.
47 bottles of beer on the wall, 47 bottles of beer.
Take one down, pass it around, 46 bottles of beer on the wall.
46 bottles of beer on the wall, 46 bottles of beer.
Take one down, pass it around, 45 bottles of beer on the wall.
45 bottles of beer on the wall, 45 bottles of beer.
Take one down, pass it around, 44 bottles of beer on the wall.
44 bottles of beer on the wall, 44 bottles of beer.
Take one down, pass it around, 43 bottles of beer on the wall.
43 bottles of beer on the wall, 43 bottles of beer.
Take one down, pass it around, 42 bottles of beer on the wall.
42 bottles of beer on the wall, 42 bottles of beer.
Take one down, pass it around, 41 bottles of beer on the wall.
41 bottles of beer on the wall, 41 bottles of beer.
Take one down, pass it around, 40 bottles of beer on the wall.
40 bottles of beer on the wall, 40 bottles of beer.
Take one down, pass it around, 39 bottles of beer on the wall.
39 bottles of beer on the wall, 39 bottles of beer.
Take one down, pass it around, 38 bottles of beer on the wall.
38 bottles of beer on the wall, 38 bottles of beer.
Take one down, pass it around, 37 bottles of beer on the wall.
37 bottles of beer on the wall, 37 bottles of beer.
Take one down, pass it around, 36 bottles of beer on the wall.
36 bottles of beer on the wall, 36 bottles of beer.
Take one down, pass it around, 35 bottles of beer on the wall.
35 bottles of beer on the wall, 35 bottles of beer.
Take one down, pass it around, 34 bottles of beer on the wall.
34 bottles of beer on the wall, 34 bottles of beer.
Take one down, pass it around, 33 bottles of beer on the wall.
33 bottles of beer on the wall, 33 bottles of beer.
Take one down, pass it around, 32 bottles of beer on the wall.
32 bottles of beer on the wall, 32 bottles of beer.
Take one down, pass it around, 31 bottles of beer on the wall.
31 bottles of beer on the wall, 31 bottles of beer.
Take one down, pass it around, 30 bottles of beer on the wall.
30 bottles of beer on the wall, 30 bottles of beer.
Take one down, pass it around, 29 bottles of beer on the wall.
29 bottles of beer on the wall, 29 bottles of beer.
Take one down, pass it around, 28 bottles of beer on the wall.
28 bottles of beer on the wall, 28 bottles of beer.
Take one down, pass it around, 27 bottles of beer on the wall.
27 bottles of beer on the wall, 27 bottles of beer.
Take one down, pass it around, 26 bottles of beer on the wall.
26 bottles of beer on the wall, 26 bottles of beer.
Take one down, pass it around, 25 bottles of beer on the wall.
25 bottles of beer on the wall, 25 bottles of beer.
Take one down, pass it around, 24 bottles of beer on the wall.
24 bottles of beer on the wall, 24 bottles of beer.
Take one down, pass it around, 23 bottles of beer on the wall.
23 bottles of beer on the wall, 23 bottles of beer.
Take one down, pass it around, 22 bottles of beer on the wall.
22 bottles of beer on the wall, 22 bottles of beer.
Take one down, pass it around, 21 bottles of beer on the wall.
21 bottles of beer on the wall, 21 bottles of beer.
Take one down, pass it around, 20 bottles of beer on the wall.
20 bottles of beer on the wall, 20 bottles of beer.
Take one down, pass it around, 19 bottles of beer on the wall.
19 bottles of beer on the wall, 19 bottles of beer.
Take one down, pass it around, 18 bottles of beer on the wall.
18 bottles of beer on the wall, 18 bottles of beer.
Take one down, pass it around, 17 bottles of beer on the wall.
17 bottles of beer on the wall, 17 bottles of beer.
Take one down, pass it around, 16 bottles of beer on the wall.
16 bottles of beer on the wall, 16 bottles of beer.
Take one down, pass it around, 15 bottles of beer on the wall.
15 bottles of beer on the wall, 15 bottles of beer.
Take one down, pass it around, 14 bottles of beer on the wall.
14 bottles of beer on the wall, 14 bottles of beer.
Take one down, pass it around, 13 bottles of beer on the wall.
13 bottles of beer on the wall, 13 bottles of beer.
Take one down, pass it around, 12 bottles of beer on the wall.
12 bottles of beer on the wall, 12 bottles of beer.
Take one down, pass it around, 11 bottles of beer on the wall.
11 bottles of beer on the wall, 11 bottles of beer.
Take one down, pass it around, 10 bottles of beer on the wall.
10 bottles of beer on the wall, 10 bottles of beer.
Take one down, pass it around, 9 bottles of beer on the wall.
9 bottles of beer on the wall, 9 bottles of beer.
Take one down, pass it around, 8 bottles of beer on the wall.
8 bottles of beer on the wall, 8 bottles of beer.
Take one down, pass it around, 7 bottles of beer on the wall.
7 bottles of beer on the wall, 7 bottles of beer.
Take one down, pass it around, 6 bottles of beer on the wall.
6 bottles of beer on the wall, 6 bottles of beer.
Take one down, pass it around, 5 bottles of beer on the wall.
5 bottles of beer on the wall, 5 bottles of beer.
Take one down, pass it around, 4 bottles of beer on the wall.
4 bottles of beer on the wall, 4 bottles of beer.
Take one down, pass it around, 3 bottles of beer on the wall.
3 bottles of beer on the wall, 3 bottles of beer.
Take one down, pass it around, 2 bottles of beer on the wall.
2 bottles of beer on the wall, 2 bottles of beer.
Take one down, pass it around, 1 bottles of beer on the wall.
1 bottles of beer on the wall, 1 bottles of beer.
Take one down, pass it around, 0 bottles of beer on the wall.

Write a function char_freq() that takes a string and builds a frequency listing of the characters contained in it. Represent the frequency listing as a dictionary. Try it with something like char_freq("abbabcbdbabdbdbabababcbcbab").


In [31]:
def char_freq(word):
    dictionary = {}
    for letter in word:
        if letter not in dictionary:
            dictionary[letter] = 1
        else:
            dictionary[letter] += 1
    return dictionary
char_freq('abbabcbdbabdbdbabababcbcbab')


Out[31]:
{'a': 7, 'b': 14, 'c': 3, 'd': 3}

In [29]:
def char_freq(word):
    dictionary = {}
    for letter in set(word):
        dictionary[letter] = word.count(letter)
    return dictionary
char_freq('abbabcbdbabdbdbabababcbcbab')


Out[29]:
{'a': 7, 'b': 14, 'c': 3, 'd': 3}

Slightly more difficult ones

Write a function that will calculate the average word length of a text stored in a file (i.e the sum of all the lengths of the word tokens in the text, divided by the number of word tokens). Add an option to exclude blank lines or chapter headers from the computation.

Use the aristotle.txt file contained in the data directory


In [32]:
def average_word_length(input_file, skip=False):
    words = []
    for line in open(input_file, 'r'):
        # remove the newline character
        line = line.rstrip()
        if skip is True:
            # Only one character means empty line
            if len(set(line)) == 1:
                continue
        # go word by word
        for word in line.split():
            # skip "empty" words
            if len(word) == 0:
                continue
            words.append(len(word))
    total_words_length = 0
    for word_len in words:
        total_words_length += word_len
    return float(total_words_length)/len(words)

In [33]:
average_word_length('../data/aristotle.txt')


Out[33]:
4.706698374950456

In [34]:
average_word_length('../data/aristotle.txt', skip=True)


Out[34]:
4.708168120539255

If all the previous exercises sounded boring to you

An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; e.g., orchestra = carthorse. Using the word list in the unixdict.txt file (data directory), write a program that finds the sets of words that share the same characters that contain the most words in them.


In [39]:
def anagrams(infile):
    words = {}
    for line in open(infile):
        word = line.rstrip()
        # dictionary is KEY:VALUE
        # key is list of unique letters
        # value is a set of words
        words[tuple(set(word))] = words.get(tuple(set(word)), set())
        words[tuple(set(word))].add(word)
    # return the longest set of words
    longest = set()
    for key, value in words.items():
        if len(value) > len(longest):
            longest = value
    return longest

In [41]:
def anagrams(infile):
    words = {}
    for line in open(infile):
        word = line.rstrip()
        # dictionary is KEY:VALUE
        # key is list of unique letters
        # value is a set of words
        words[tuple(set(word))] = words.get(tuple(set(word)), set())
        words[tuple(set(word))].add(word)
    # return the longest set of words
    return sorted(words.values(), key=lambda x: len(x))[-1]

In [42]:
anagrams('../data/unixdict.txt')


Out[42]:
{'caret',
 'carte',
 'cater',
 'cetera',
 'crate',
 'crater',
 'create',
 'retract',
 'teacart',
 'terrace',
 'trace'}